# VDH 项目 Makefile
# 简化构建、测试和部署过程

.PHONY: all build clean test install uninstall help

# 默认目标
all: build

# ===========================================
# 构建相关
# ===========================================

# 构建项目
build:
	@echo "🔨 Building VDH..."
	xcodebuild -scheme vdh_tool -configuration Release
	@echo "✅ Build completed"

# 构建调试版本
build-debug:
	@echo "🔨 Building VDH (Debug)..."
	xcodebuild -scheme vdh_tool -configuration Debug
	@echo "✅ Debug build completed"

# 清理构建产物
clean:
	@echo "🧹 Cleaning build artifacts..."
	xcodebuild clean
	rm -rf build/
	rm -rf ~/Library/Developer/Xcode/DerivedData/vdh_tool-*
	@echo "✅ Clean completed"

# ===========================================
# 测试相关
# ===========================================

# 运行基本功能测试
test: build-debug
	@echo "🧪 Running basic tests..."
	./build/Debug/vdh_tool --version
	@echo "✅ Basic tests passed"

# 运行集成测试
test-integration: build-debug
	@echo "🔗 Running integration tests..."
	@echo "Starting server..."
	./build/Debug/vdh_tool start
	@sleep 2
	@echo "Testing download request..."
	./build/Debug/vdh_tool input "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
	@sleep 5
	@echo "Checking status..."
	./build/Debug/vdh_tool status
	@echo "Stopping server..."
	./build/Debug/vdh_tool stop
	@echo "✅ Integration tests completed"

# ===========================================
# 安装相关
# ===========================================

# 安装到系统 (需要 sudo)
install: build
	@echo "📦 Installing VDH to system..."
	sudo cp build/Release/vdh_tool /usr/local/bin/vdh
	sudo chmod +x /usr/local/bin/vdh
	@echo "✅ VDH installed to /usr/local/bin/vdh"
	@echo "💡 Run 'vdh --help' to get started"

# 卸载
uninstall:
	@echo "🗑️ Uninstalling VDH..."
	sudo rm -f /usr/local/bin/vdh
	@echo "✅ VDH uninstalled"

# ===========================================
# 开发工具
# ===========================================

# 启动开发服务器
dev-server: build-debug
	@echo "🚀 Starting development server..."
	./build/Debug/vdh_tool server

# 检查代码质量
lint:
	@echo "🔍 Checking code quality..."
	@echo "SwiftLint not configured yet"
	@echo "Consider adding SwiftLint for code quality checks"

# 生成文档
docs:
	@echo "📚 Generating documentation..."
	@echo "Documentation generation not implemented yet"
	@echo "See README.md and DEVELOPMENT.md for current docs"

# ===========================================
# 部署相关
# ===========================================

# 创建发布包
package: build
	@echo "📦 Creating release package..."
	mkdir -p dist
	cp build/Release/vdh_tool dist/vdh
	cp README.md dist/
	cp QUICKSTART.md dist/
	cp DEVELOPMENT.md dist/
	cp config.yaml.template dist/config.yaml.example
	cd dist && tar -czf vdh-$(shell date +%Y%m%d).tar.gz *
	@echo "✅ Release package created: dist/vdh-$(shell date +%Y%m%d).tar.gz"

# ===========================================
# 维护相关
# ===========================================

# 更新依赖
update-deps:
	@echo "📦 Updating dependencies..."
	brew update
	brew upgrade yt-dlp
	@echo "✅ Dependencies updated"

# 检查系统要求
check-system:
	@echo "🔍 Checking system requirements..."
	@echo "macOS Version: $(shell sw_vers -productVersion)"
	@echo "Xcode Version: $(shell xcodebuild -version | head -1)"
	@echo "Swift Version: $(shell swift --version | head -1)"
	@if command -v yt-dlp >/dev/null 2>&1; then \
		echo "yt-dlp Version: $(shell yt-dlp --version)"; \
	else \
		echo "❌ yt-dlp not found. Install with: brew install yt-dlp"; \
		exit 1; \
	fi
	@echo "✅ System check completed"

# 显示项目信息
info:
	@echo "ℹ️ VDH Project Information"
	@echo "Version: 2.0.0"
	@echo "Swift Version: 5.9+"
	@echo "macOS Target: 14.0+"
	@echo "Dependencies: yt-dlp"
	@echo "License: MIT"
	@echo ""
	@echo "Project Structure:"
	@find . -name "*.swift" -type f | wc -l | xargs echo "  Swift files:"
	@echo "  Build system: Xcode"
	@echo "  Architecture: Modular CLI tool"

# ===========================================
# 帮助信息
# ===========================================

help:
	@echo "VDH 项目构建工具"
	@echo ""
	@echo "可用目标:"
	@echo "  build          - 构建发布版本"
	@echo "  build-debug    - 构建调试版本"
	@echo "  clean          - 清理构建产物"
	@echo "  test           - 运行基本测试"
	@echo "  test-integration - 运行集成测试"
	@echo "  install        - 安装到系统 (/usr/local/bin)"
	@echo "  uninstall      - 从系统卸载"
	@echo "  dev-server     - 启动开发服务器"
	@echo "  package        - 创建发布包"
	@echo "  update-deps    - 更新依赖"
	@echo "  check-system   - 检查系统要求"
	@echo "  info           - 显示项目信息"
	@echo "  docs           - 生成文档"
	@echo "  lint           - 代码质量检查"
	@echo "  help           - 显示此帮助信息"
	@echo ""
	@echo "示例用法:"
	@echo "  make build && make install    # 构建并安装"
	@echo "  make test-integration         # 运行完整测试"
	@echo "  make package                  # 创建发布包"
	@echo ""
	@echo "快速开始:"
	@echo "  make check-system && make build && make install"

# 默认目标的别名
.DEFAULT_GOAL := help